const {Controller} = require('egg');

class HomeController extends Controller{
    async add(){
        let {ctx} = this;
        let {username,password,idcard,address,phone,sex} = ctx.request.body;
        if(!username || !password || !idcard || !address){
            ctx.body = {
                code:1,
                msg:'丢失参数'
            }
        }else{
            //查此人是否存在
            let userData = await this.service.user.selectUser(idcard);
            if(userData.length){
                ctx.body = {
                    code:3,
                    msg:'此人已存在'
                }
            }else{
                // //插入数据
                try{
                    let create_time = new Date();
                    await this.service.user.add(username,password,idcard,address,phone,sex,create_time);
                    ctx.body = {
                        code:1,
                        msg:'添加成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }
    }
    async getList(){
        let {ctx,service} = this;
        try{
            let {pagenum=1,limit=2} = ctx.query;
            let startIndex = (pagenum-1)*limit;
            let data = await service.user.getList(startIndex,limit);
            ctx.body = {
                code:1,
                data
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }
}

module.exports = HomeController